import * as zrender from 'zrender'
class Scale {
    constructor(config, chartData) {
        this.scaleConfig = {
            type: 'linear',
            // 向外扩展一定范围，linear有效
            domainScale: 1.1,
            // 是否一定显示0
            showZero: true,
        }
        this.config = config
        this.chartData = chartData;
        zrender.util.merge(this.scaleConfig, config.scale || {}, true)
        config.scale = this.scaleConfig
        this[this.scaleConfig.type + 'Scale']();
        return this
    }

    // 线性轴
    linearScale() {
        let data = this.chartData.map((item) => {
            return item[this.config.dataIndex]
        })
        let max = Math.max(...data)
        let min = Math.min(...data)
        // 做扩展范围（domainScale）
        let padding = (max - min) * (this.scaleConfig.domainScale - 1) / 2
        max += padding;
        min < 0 && (min -= padding);
        // 做0的固定显示
        if(this.scaleConfig.showZero){
            if (min > 0) min = 0;
            else if(max < 0) max = 0;
        }

        this.max = max;
        this.min = min;
        this.get = (val) => {
            if(val < min || val > max) console.error('Scale: 数据超出坐标轴范围');
            let base = this.config.start[this.config.XorY === 'x' ? 0 : 1]
            return base + (this.config.XorY === 'x' ? 1 : -1) * (val - min) / (max - min) * this.config.length;
        }
    }

    // 点轴
    pointScale() {

    }
}

export default Scale